package cn.com.nes.site.entity.em;

import cn.com.nes.common.exception.MissBusinessParameterException;
import cn.hutool.core.util.ObjectUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 工单状态
 * @author lux
 */
public enum WorkOrderStautsEnum {
    /**
     * 待派单
     */
    _0("0", "待派单", false),
    /**
     * 待支援
     */
    _1("1", "待支援", false),
    /**
     * 处理中
     */
    _2("2", "处理中", false),
    /**
     * 已完成
     */
    _3("3", "已完成", true),
    /**
     * 已关闭
     */
    _4("4", "已关闭", false),
    /**
     * 已拒单
     */
    _5("5", "已拒单", false),
    /**
     * 维修-待回访
     */
    _6("6", "待回访", true),
    /**
     * 安装 -待审核
     */
    _7("7", "待审核", true),
    /**
     * 安装 - 已分单
     */
    _8("8", "已分单", false),
    /**
     * 安装 - 已分单
     */
    _9("9", "已撤销", false),

    ;

    private String key;

    private String val;

    @Getter
    private Boolean isComplete; // 当卡状态码对应的工单是否已安装完成

    WorkOrderStautsEnum(String key, String val, Boolean isComplete) {
        this.key = key;
        this.val = val;
        this.isComplete = isComplete;
    }

    public String getVal() {
        return val;
    }

    public String getKey() {
        return key;
    }

    public static String getVal(Integer key) {
        Map<String, String> map = Arrays.stream(values())
                .collect(Collectors.toMap(WorkOrderStautsEnum::getKey, WorkOrderStautsEnum::getVal));
        return ObjectUtil.isNull(key) ? "" : map.get(key.toString());
    }

    // 获取安装完成状态对应值的列表
    public static List<String> getStatusList(Boolean isComplete) {
        if (ObjectUtil.isNull(isComplete)) {
            throw new MissBusinessParameterException("isComplete must not null");
        }

        return Arrays.stream(values())
                .filter(n -> isComplete.equals(n.getIsComplete()))
                .map(WorkOrderStautsEnum::getKey)
                .collect(Collectors.toList());
    }

    // 按状态获取工单是否已完成
    public static Boolean getIsComplete(Integer key) {
        Map<String, Boolean> map = Arrays.stream(values())
                .collect(Collectors.toMap(WorkOrderStautsEnum::getKey, WorkOrderStautsEnum::getIsComplete));
        return !ObjectUtil.isNull(key) && map.get(key.toString());
    }

}
